const path = require('path');
const { sep } = path;
const glob = require('glob');

module.exports = (app) => {
    const extendPath = path.resolve(app.bisinessPath, `.${sep}extends`);
    const fileList = glob.sync(path.resolve(extendPath, `.${sep}**${sep}**.js`));

    fileList.forEach((file) => {
        let name = path.resolve(file)
        name = name.substring(name.lastIndexOf(`extends${sep}`) + `extends${sep}`.length, name.lastIndexOf('.'))
        name = name.replace(/[_-][a-z]/ig, (s) => s.substring(1).toUpperCase())

        for(key in app) {
            if(key == name) {
                console.log(`[extend] load error: ${key} is already in app`);
                return;
            }
        }
        app[name] = require(path.resolve(file))(app)
    });
}